# coding=utf-8
# 本题为考试多行输入输出规范示例，无需提交，不计分。
# ACM模式
# 分配糖果问题，和leetcode 135基本相同，多了个环
# 过了90%
import sys

def getRes(ages):
    papers = [1 for _ in range(len(ages))]
    # from left
    for i in range(len(ages)):
        if i == 0:
            if ages[i] > ages[-1]:
                papers[i] = papers[-1] + 1
        else:
            if ages[i] > ages[i-1]:
                papers[i] = papers[i-1] + 1
    
    # from right
    for i in range(len(ages)):
        i = len(ages) - 1 - i
        if i == len(ages) - 1:
            if ages[i] > ages[0] and papers[i] <= papers[0]:
                papers[i] = papers[0] + 1
        else:
            if ages[i] > ages[i+1] and papers[i] <= papers[i+1]:
                papers[i] = papers[i+1] + 1

    # 等第一个人更新完之后，检查一下最后一个人和第一个人的条件是否满足            
    if ages[-1] > ages[0] and papers[i] <= papers[0]:
            papers[-1] = papers[0] + 1
    return papers


if __name__ == "__main__":
    # 读取第一行的n
    line = sys.stdin.readline().strip()
    ages = list(map(int, line.split()))

    papers = getRes(ages)
    # print(papers)
    print(sum(papers))
    

